<?php

namespace app\controller\front;

use app\BaseController;
use app\model\DingUser as DingUserModel;
use app\model\Photo as PhotoModel;
use app\model\DingGroup as DingGroupModel;
use app\lib\exception\VerifyException;
class Roster extends BaseController
{

    // 前台登录
    public function login()
    {

        $params = request()->param();
        $mobile = $params['mobile'];
        $name = $params['realname'];

        $dingUserModel = new DingUserModel;
        $info = $dingUserModel->where(['mobile' => $mobile,'name' => $name])->find();

        if(empty($info)){
            throw new VerifyException();
        }

        return $this->successMessage('登陆成功',['id' => $info['id']]);   

    }


    public function getRoters()
    {
        $params = request()->param();
        $sign = $params['sign'];

        $appSecret = '&*&UUFE6688';
        $create_time = $params['create_time']; 

        $tmpArr = array($create_time, $appSecret);
        sort($tmpArr, SORT_STRING);
        $tmpStr = sha1(implode( $tmpArr ));
    
        if(!strstr($tmpStr, $sign)){
            throw new VerifyException();
        }

        $dingUserModel = new DingUserModel;
        $res = $dingUserModel->where(['is_quit' => 0])->select();

        $arr = [];

        foreach ($res as $key => $item) {
            $_arr = [];
            $_arr['unionid'] = '';
            $_arr['openId'] = '';
            $_arr['remark'] = '';
            $_arr['userid'] = $item['id'];
            $_arr['isBoss'] = 0;
            $_arr['hiredDate'] = $item['hiredDate'];
            $_arr['tel'] = '';
            $_arr['department'] = $item['department'];
            $_arr['workPlace'] = ''; 
            $_arr['email'] = '';
            $_arr['order'] = '';
            $_arr['dingId'] = '';
            $_arr['isLeader'] = 0;
            $_arr['mobile'] = $item['mobile'];
            $_arr['active'] = 1;
            $_arr['isAdmin'] = 0;
            $_arr['avatar'] = $item['avatar'];
            $_arr['isHide'] = 0;
            $_arr['jobnumber'] = $item['jobnumber'];
            $_arr['name'] = $item['name'];
            $_arr['extattr'] = '{}';
            $_arr['stateCode'] = '';
            $_arr['position'] = $item['position'];
            $arr[] = $_arr;
        }

        return $this->successMessage('查询成功',$arr);        
    }


    public function getUid()
    {
        $params = request()->param();
        $dingid = $params['dingid'];
        $dingUserModel = new DingUserModel;
        $res = $dingUserModel->where(['id' => $dingid])->find();
        return $this->successMessage('查询成功',$res);
    }

    public function getRosterInfo()
    {
        $params = request()->param();
        $uid = $params['uid'];
        $res = DingUserModel::find($uid);
        $dingGroupModel = new DingGroupModel;
        $res['group'] = $dingGroupModel->find(explode(',', $res['department'])[0])['name'];

        return $this->successMessage('查询成功',$res);
    }


    public function getPhotoList()
    {
        $params = request()->param();
        $uid = $params['uid'];
        $photoModel = new PhotoModel;
        $res = $photoModel->where(['user_id' => $uid])->select();
        return $this->successMessage('查询成功',$res);

    }


}